package com.biyao.service.model.state.imp;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.*;

import com.biyao.schedulingdao.state.TRollerExtMapper;
import com.biyao.schedulingentity.base.TBearingBoxEntity;
import com.biyao.schedulingentity.base.TGrinderEntity;
import com.biyao.schedulingentity.base.TRollerRacksEntity;
import com.biyao.schedulingentity.product.TProductionPlanIEntity;
import com.biyao.schedulingentity.state.*;
import com.biyao.service.model.base.*;
import com.biyao.service.model.product.ProductionPlanIMessage;
import com.biyao.service.model.state.RollerExtMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 *
 * @author code generator
 * @date 2019-09-16 09:44:10
 */
@Service
public class TRollerExtService extends ServiceImpl<TRollerExtMapper, TRollerExtEntity> implements RollerExtMessage {

    @Autowired
    private TRollerRacksMessage tRollerRacksMessage;

    @Autowired
    private TRollerBasicMessage tRollerBasicMessage;
    @Autowired
    private TBearingBoxMessage tBearingBoxMessage;

    @Autowired
    private ProductionPlanIMessage productionPlanIMessage;

    @Autowired
    private TGrinderMessage tGrinderMessage;
    public Page list(int pageNum, int pageSize){

        EntityWrapper<TRollerExtEntity> tRollerExtentityWrapper=new EntityWrapper<>();
        Page page=selectPage(new Page(pageNum,pageSize),tRollerExtentityWrapper);
        page.setRecords(formatRecords(page.getRecords()));
        return page;
    }

    List formatRecords(List<TRollerExtEntity> list){
        List resList=new ArrayList();
        for(TRollerExtEntity tRollerExtEntity:list){
            Map map=formatEntity(tRollerExtEntity);
            resList.add(map);
        }
        return resList;
    }

    Map formatEntity(TRollerExtEntity tRollerExtEntity){
        Map map=new HashMap(16);
        //轧辊当前信息
        if(tRollerExtEntity.getId()!=null){
           map.put("id",tRollerExtEntity.getId());
        }
        //t_roller_basic.id
        if(tRollerExtEntity.getRollerId()!=null){
           map.put("rollerId",tRollerExtEntity.getRollerId());
        }
        //轧辊编号
        if(tRollerExtEntity.getCode()!=null){
           map.put("code",tRollerExtEntity.getCode());
        }
        //轧辊工作状态 1- 未磨热辊；2- 未磨冷辊；3- 已磨未上油辊；4- 已磨已上油辊
        if(tRollerExtEntity.getWorkingState()!=null){
           map.put("workingState",tRollerExtEntity.getWorkingState());
        }
        if(tRollerExtEntity.getGrinderId()!=null){
            map.put("grinderId",tRollerExtEntity.getGrinderId());
            TGrinderEntity tGrinderEntity= tGrinderMessage.selectEntryById(tRollerExtEntity.getGrinderId());
            map.put("rollerBackCode",tGrinderEntity.getCode());
            map.put("rollerBackId",tRollerExtEntity.getGrinderId());

        }
        //上架或是下架 1-上架; 2-下架
        if(tRollerExtEntity.getRollerRackUOrD()!=null){
           map.put("rollerRackUOrD",tRollerExtEntity.getRollerRackUOrD());
        }
        if(tRollerExtEntity.getRollerRackId()!=null){
            map.put("rollerBackId",tRollerExtEntity.getRollerRackId());
            map.put("rollerBackCode",tRollerRacksMessage.selectTRollerEntityById(tRollerExtEntity.getRollerRackId()).getCode());
        }

        //当前直径
        if(tRollerExtEntity.getDiameter()!=null){
           map.put("diameter",tRollerExtEntity.getDiameter());
        }
        //表面粗糙度
        if(tRollerExtEntity.getRoughness()!=null){
           map.put("roughness",tRollerExtEntity.getRoughness());
        }
        //辊型精度
        if(tRollerExtEntity.getPrecision()!=null){
           map.put("precision",tRollerExtEntity.getPrecision());
        }
        //
        if(tRollerExtEntity.getRemark()!=null){
           map.put("remark",tRollerExtEntity.getRemark());
        }
        if(tRollerExtEntity.getMileage()!=null){
            map.put("mileage",tRollerExtEntity.getMileage());
        }
        if(tRollerExtEntity.getRollerState()!=null){
            map.put("rollerState",tRollerExtEntity.getRollerState());
        }
        if(tRollerExtEntity.getParRollerCode()!=null){
            map.put("parRollerCode",tRollerExtEntity.getParRollerCode());
        }
        if(tRollerExtEntity.getOperateBox()!=null){
            map.put("operateBox",tRollerExtEntity.getOperateBox());
        }
        if(tRollerExtEntity.getOperateBear()!=null){
            map.put("operateBear",tRollerExtEntity.getOperateBear());
        }
        if(tRollerExtEntity.getTranBear()!=null){
            map.put("tranBear",tRollerExtEntity.getTranBear());
        }
        if(tRollerExtEntity.getTranBox()!=null){
            map.put("tranBox",tRollerExtEntity.getTranBox());

        }
        if(tRollerExtEntity.getMaintainTime()!=null){
            map.put("maintainTime",tRollerExtEntity.getMaintainTime());
        }
        if(tRollerExtEntity.getNote()!=null){
            map.put("note",tRollerExtEntity.getNote());
        }

//        //
        if(tRollerExtEntity.getCreateTime()!=null){
           map.put("createTime",tRollerExtEntity.getCreateTime());
        }
//        //
//        if(tRollerExtEntity.getUpdateTime()!=null){
//           map.put("updateTime",tRollerExtEntity.getUpdateTime());
//        }
//        //
//        if(tRollerExtEntity.getCreateBy()!=null){
//           map.put("createBy",tRollerExtEntity.getCreateBy());
//        }
//        //
//        if(tRollerExtEntity.getUpdateBy()!=null){
//           map.put("updateBy",tRollerExtEntity.getUpdateBy());
//        }
        return map;
    }

    @Override
    public Map updateDynamics(TRollerExtEntity tRollerExtEntity) {
        Map map=new HashMap();
        //判断辊架上是否有辊
        EntityWrapper<TRollerExtEntity> tRollerExtentityWrapper=new EntityWrapper<>();
        tRollerExtentityWrapper.eq("code",tRollerExtEntity.getCode());
        List<TRollerExtEntity> list=selectList(tRollerExtentityWrapper);
        if(list.size()>0){
            map.put("msg","该轧辊已经被使用");
            map.put("flag",false);
            return map;
        }
        EntityWrapper<TRollerExtEntity> tRollerExtentityWrapper2=new EntityWrapper<>();
        tRollerExtentityWrapper2.eq("roller_rack_id",tRollerExtEntity.getRollerRackId()).eq("roller_rack_u_or_d",tRollerExtEntity.getRollerRackUOrD());
        List<TRollerExtEntity> list2=selectList(tRollerExtentityWrapper2);

        if(list2.size()>0){
            map.put("msg","该辊架位置上已经有辊");
            map.put("flag",false);
            return map;
        }

        map.put("flag",true);
        insert(tRollerExtEntity);
        return map;
    }

    @Override
    public List<Map> selectNowState() {
        EntityWrapper<TRollerExtEntity> tRollerExtentityWrapper=new EntityWrapper<>();
        List<TRollerExtEntity> list=selectList(tRollerExtentityWrapper);
        List<Map> backList=formatRecords(list);
        HashMap<Integer,Map> hashMap=new HashMap();
        for(Map map:backList){
            if(hashMap.get(map.get("rollerBackId"))!=null){
                Map map1=hashMap.get(map.get("rollerBackId"));
                String code1=(String)map.get("code");
                String code2=(String)map1.get("code");
                map.put("oneOreTwo",2);
                map.put("title",code1+","+code2);
                map1.put("oneOreTwo",2);
                map1.put("title",code1+","+code2);
            }else {
                hashMap.put((Integer) map.get("rollerBackId"),map);
                map.put("oneOreTwo",1);
                map.put("title",map.get("code"));
            }
        }
        return backList;
    }

    @Override
    public Map selectNowStateByCode(Integer ids) {
        EntityWrapper<TRollerExtEntity> tRollerExtentityWrapper=new EntityWrapper<>();
        tRollerExtentityWrapper.eq("roller_id",ids);
        List<TRollerExtEntity> list=selectList(tRollerExtentityWrapper);
        if(list.size()>0){
            TRollerExtEntity tRollerExtEntity=list.get(0);
            if(tRollerExtEntity.getDiameter()==null){
                Float diamaterNow=tRollerBasicMessage.selectRollerBasicDiameter(tRollerExtEntity.getRollerId());
                tRollerExtEntity.setDiameter(diamaterNow);
            }
            Map map=formatEntity(tRollerExtEntity);
            TBearingBoxEntity tBearingBoxEntity= tBearingBoxMessage.selectBearingBoxById(ids);
            if(tBearingBoxEntity!=null){
                map.put("bearing_box",tBearingBoxEntity.getCode());
            }

            return map;
        }
        return null;
    }

    @Override
    @Transactional
    public Map moveRollerState(RollerMoveEntityList rollerMoveEntityList) {
        synchronized (this) {
            if (rollerMoveEntityList.getProductionIId() != null) {
                TProductionPlanIEntity tProductionPlanIEntity = new TProductionPlanIEntity();
                tProductionPlanIEntity.setId(rollerMoveEntityList.getProductionIId());
                tProductionPlanIEntity.setProductionPlanId(rollerMoveEntityList.getProductionPlanId());
                tProductionPlanIEntity.setSequence(rollerMoveEntityList.getSequence());
                String param0=productionPlanIMessage.selectProductionIId(rollerMoveEntityList.getProductionIId()).getParam0();
                if(StringUtils.isEmpty(param0)){
                    productionPlanIMessage.updateNowProductionPlanIPostion(tProductionPlanIEntity);
                    productionPlanIMessage.updateNextProductionPlanIPostion(tProductionPlanIEntity);
                }
            }
            if (rollerMoveEntityList.getList().size() > 0) {
                for (RollerMoveEntity rollerMoveEntity : rollerMoveEntityList.getList()) {
                    Map map = judgeIsMoveToRollerRacks(rollerMoveEntity);
                    if (!(Boolean) map.get("flag")) {
                        return map;
                    }
                    //删除该辊在原辊架位置 或者是磨床
                    if (rollerMoveEntity.getSourceRollerBackId() != null && rollerMoveEntity.getSourceRollerRackUorD() != null) {
                        EntityWrapper<TRollerExtEntity> tRollerExtEntityEntityWrapper = new EntityWrapper<>();
                        tRollerExtEntityEntityWrapper.eq("roller_id", rollerMoveEntity.getRollerId());
                        delete(tRollerExtEntityEntityWrapper);
                    }
                    //添加轧辊的新位置
                    TRollerExtEntity tRollerExtEntity = new TRollerExtEntity();
                    tRollerExtEntity.setCode(rollerMoveEntity.getCode());
                    tRollerExtEntity.setRollerId(rollerMoveEntity.getRollerId());
                    tRollerExtEntity.setRollerRackId(rollerMoveEntity.getTargetRollerBackId());
                    tRollerExtEntity.setRollerRackUOrD(rollerMoveEntity.getTargetRollerRackUorD());
                    tRollerExtEntity.setWorkingState(rollerMoveEntity.getWorkingState());
                    tRollerExtEntity.setGrinderId(rollerMoveEntity.getGrinderId());
                    insert(tRollerExtEntity);
                }
                Map map = new HashMap();
                map.put("flag", true);
                return map;
            } else {
                Map map = new HashMap();
                map.put("flag", false);
                return map;
            }
        }

    }

    @Override
    public void updateRollerExt(RollerMoveEntity rollerMoveEntity) {
        baseMapper.updateRollerState(rollerMoveEntity.getGrinderId(),rollerMoveEntity.getWorkingState(),rollerMoveEntity.getDiameter(),rollerMoveEntity.getRollerId(),rollerMoveEntity.getSourceRollerRackUorD());
    }

    @Override
    public void updateGrinderToRollerExt(RollerMoveEntity rollerMoveEntity) {
        baseMapper.updateGrinderToRollerState(rollerMoveEntity.getWorkingState(),rollerMoveEntity.getTargetRollerBackId(),rollerMoveEntity.getTargetRollerRackUorD(),rollerMoveEntity.getRollerId(),rollerMoveEntity.getGrinderId());
    }


    public Map judgeIsMoveToRollerRacks(RollerMoveEntity rollerMoveEntity) {
        Map map=new HashMap();
        EntityWrapper<TRollerExtEntity> tRollerExtentityWrapper=new EntityWrapper<>();
        if(rollerMoveEntity.getTargetRollerBackId() != null){
            tRollerExtentityWrapper.eq("roller_rack_id",rollerMoveEntity.getTargetRollerBackId()).eq("roller_rack_u_or_d",rollerMoveEntity.getTargetRollerRackUorD());
        }else if(rollerMoveEntity.getGrinderId() != null){
            tRollerExtentityWrapper.eq("grinder_id",rollerMoveEntity.getGrinderId()).eq("roller_rack_u_or_d",rollerMoveEntity.getTargetRollerRackUorD());
        }
        List<TRollerExtEntity> list=selectList(tRollerExtentityWrapper);
        if(list.size()>0){
            if(rollerMoveEntity.getTargetRollerBackId() != null){
                map.put("msg","该辊架位置上已经有辊");
            }else if(rollerMoveEntity.getGrinderId() != null){
                map.put("msg","该磨床位置上已经有辊");
            }
            map.put("flag",false);
            return map;
        }
        map.put("flag",true);
        return map;
    }

    @Override
    public Float selectRollerExtNowDiameter(Integer rollerId) {
        //先查询轧辊当前状态表中是否有该轧辊
        EntityWrapper<TRollerExtEntity> tRollerExtentityWrapper=new EntityWrapper<>();
        tRollerExtentityWrapper.eq("roller_id",rollerId);
        List<TRollerExtEntity> list=selectList(tRollerExtentityWrapper);
        Float diamaterNow=null;
        if(list.size()>0){
            diamaterNow=list.get(0).getDiameter();
            if(diamaterNow==null){
                diamaterNow=tRollerBasicMessage.selectRollerBasicDiameter(rollerId);
            }
        }
        return diamaterNow;
    }

    @Override
    public List<Map> selectRollerExtMessage() {
        return baseMapper.selectRollerExtMessage();
    }

    @Override
    public List<TRollerExtCopyEntity> selectNowWorkState3() {
        List<TRollerExtCopyEntity> backList=new ArrayList<>();
        EntityWrapper<TRollerExtEntity> tRollerExtentityWrapper=new EntityWrapper<>();
        tRollerExtentityWrapper.eq("working_state",3).or().eq("working_state",4);
        List<TRollerExtEntity> list=selectList(tRollerExtentityWrapper);
        for(TRollerExtEntity tRollerExtEntity:list){
            TRollerExtCopyEntity tRollerExtCopyEntity=new TRollerExtCopyEntity();
            BeanUtils.copyProperties(tRollerExtEntity,tRollerExtCopyEntity);
            if(tRollerExtEntity.getRollerRackId()!=null){
                TRollerRacksEntity tRollerRacksEntity=tRollerRacksMessage.selectTRollerEntityById(tRollerExtEntity.getRollerRackId());
                tRollerExtCopyEntity.setRollerBackCode(tRollerRacksEntity.getCode());
            }
            backList.add(tRollerExtCopyEntity);
        }

        HashMap<Integer,TRollerExtCopyEntity> hashMap=new HashMap();
        for(TRollerExtCopyEntity tRollerExtCopyEntity:backList){
            if(hashMap.get(tRollerExtCopyEntity.getRollerRackId())!=null){
                TRollerExtCopyEntity copy=hashMap.get(tRollerExtCopyEntity.getRollerRackId());
                copy.setOneOreTwo(2);
                tRollerExtCopyEntity.setOneOreTwo(2);
            }else {
                tRollerExtCopyEntity.setOneOreTwo(1);
                hashMap.put(tRollerExtCopyEntity.getRollerRackId(),tRollerExtCopyEntity);
            }
        }

        return backList;
    }


    @Override
    public void updateWorkState(TRollerExtEntyList list) {
        for(TRollerExtEntity tRollerExtEntity:list.getList()){
            tRollerExtEntity.setCreateTime(new Date());
            updateById(tRollerExtEntity);
        }
    }

    @Override
    public void deleteRollerExit(Integer id) {
        deleteById(id);
    }

    @Override
    public Map updateRackIdOrGrinderId(RollerMoveEntityList rollerMoveEntityList) {
        synchronized (this) {
            if (rollerMoveEntityList.getProductionIId() != null) {
                TProductionPlanIEntity tProductionPlanIEntity = new TProductionPlanIEntity();
                tProductionPlanIEntity.setId(rollerMoveEntityList.getProductionIId());
                tProductionPlanIEntity.setProductionPlanId(rollerMoveEntityList.getProductionPlanId());
                tProductionPlanIEntity.setSequence(rollerMoveEntityList.getSequence());
                productionPlanIMessage.updateNowProductionPlanIPostion(tProductionPlanIEntity);
                productionPlanIMessage.updateNextProductionPlanIPostion(tProductionPlanIEntity);
            }
            if (rollerMoveEntityList.getList().size() > 0) {
                for (RollerMoveEntity rollerMoveEntity : rollerMoveEntityList.getList()) {
                    Map map = judgeIsMoveToRollerRacks(rollerMoveEntity);
                    if (!(Boolean) map.get("flag")) {
                        return map;
                    }
                    //更新位置
                    baseMapper.updateRollerRackIdOrGrinderId(rollerMoveEntity.getGrinderId(), rollerMoveEntity.getTargetRollerBackId(), rollerMoveEntity.getRollerId());
                }
                Map map = new HashMap();
                map.put("flag", true);
                return map;
            } else {
                Map map = new HashMap();
                map.put("flag", false);
                return map;
            }
        }
    }

    @Override
    public void insertOrUpdateRollerExt(TRollerExtBaseEntity tRollerExtBaseEntity) {
        TRollerExtEntity tRollerExtEntity=new TRollerExtEntity();
        tRollerExtEntity.setId(tRollerExtBaseEntity.getId());
        tRollerExtEntity.setCode(tRollerExtBaseEntity.getCode());
        tRollerExtEntity.setRollerRackUOrD(tRollerExtBaseEntity.getRollerRackUOrD());
        tRollerExtEntity.setRollerId(tRollerExtBaseEntity.getRollerId());
        tRollerExtEntity.setRollerRackId(tRollerExtBaseEntity.getRollerBackId());
        tRollerExtEntity.setWorkingState(tRollerExtBaseEntity.getWorkingState());
        tRollerExtEntity.setRoughness(tRollerExtBaseEntity.getRoughness());
        tRollerExtEntity.setMileage(tRollerExtBaseEntity.getMileage());
        tRollerExtEntity.setRollerState(tRollerExtBaseEntity.getRollerState());
        tRollerExtEntity.setParRollerCode(tRollerExtBaseEntity.getParRollerCode());
        tRollerExtEntity.setOperateBox(tRollerExtBaseEntity.getOperateBox());
        tRollerExtEntity.setTranBox(tRollerExtBaseEntity.getTranBox());
        tRollerExtEntity.setOperateBear(tRollerExtBaseEntity.getOperateBear());
        tRollerExtEntity.setTranBear(tRollerExtBaseEntity.getTranBear());
        tRollerExtEntity.setMaintainTime(tRollerExtBaseEntity.getMaintainTime());
        tRollerExtEntity.setCreateTime(tRollerExtBaseEntity.getCreateTime());
        tRollerExtEntity.setNote(tRollerExtBaseEntity.getNote());
        if(tRollerExtBaseEntity.getId()!=null){
           updateById(tRollerExtEntity);
        }else {
            insert(tRollerExtEntity);
        }
    }

    @Override
    public Map selectRollerExtByRollerId(Integer rollerId) {
        HashMap backMap=new HashMap();
        if(rollerId!=null){
            EntityWrapper<TRollerExtEntity> tRollerExtentityWrapper=new EntityWrapper<>();
            tRollerExtentityWrapper.eq("roller_id",rollerId);
            List<TRollerExtEntity> list=selectList(tRollerExtentityWrapper);
            if(list!=null&&list.size()>0){
                TRollerExtEntity t=list.get(0);
                backMap.put("diameter",t.getDiameter());
                backMap.put("id",t.getId());
                backMap.put("rollerId",t.getRollerId());
                backMap.put("rollerRackId",t.getRollerRackId());
                backMap.put("rollerState",t.getRollerState());
                if(t.getWorkingState()!=null&&t.getWorkingState()==4){
                    backMap.put("workingState",4);
                }else {
                    backMap.put("workingState",1);

                }
                backMap.put("createTime",t.getCreateTime());
                backMap.put("parRollerCode",t.getParRollerCode());
                backMap.put("operateBox",t.getOperateBox());
                backMap.put("operateBear",t.getOperateBear());
                backMap.put("tranBox",t.getTranBox());
                backMap.put("tranBear",t.getTranBear());
            }
        }

        return backMap;
    }

    @Override
    public void updateRollerExetByRollerIdBox(TRollerExtEntity tRollerExtEntity) {
        updateById(tRollerExtEntity);
    }

}

